//
//  NewFeatureViewController.swift
//  HMWeibo06
//
//  Created by apple on 15/5/13.
//  Copyright (c) 2015年 heima. All rights reserved.
//

import UIKit

/**
    cmd+option+enter
    option+click
*/
class NewFeatureViewController: UICollectionViewController {

    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    
    // 提示：在不同的控制器中，具体设置布局属性前，一定记住跟踪几个生命周期函数
    // 确定选择一个最合适的函数
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        println("\(__FUNCTION__)  \(view.frame)")
        layout.itemSize = view.bounds.size
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
//        println("\(__FUNCTION__)  \(view.frame)")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /// 图片总数
    let imageCount = 4
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("NewFeatureCell", forIndexPath: indexPath) as! NewFeatureCell
        
        cell.imageIndex = indexPath.item
    
        return cell
    }
    
    // cell 显示完成调用
    // 注意：参数：cell&indexPath都是之前消失的那个 cell 的
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {

        // 获取正在显示的cell
        println(collectionView.indexPathsForVisibleItems())
        let path = collectionView.indexPathsForVisibleItems().last as! NSIndexPath
        
        if path.item == imageCount - 1 {
            // 根据 indexPath 获得到 cell
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
            // 播放动画
            cell.showStartButton()
        }
    }
}

class NewFeatureCell: UICollectionViewCell {
    
    @IBOutlet weak var iconImage: UIImageView!
    @IBOutlet weak var startButton: UIButton!
    
    var imageIndex: Int = 0 {
        didSet {
            // 设置图像
            iconImage.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            // 隐藏启动按钮
            startButton.hidden = true
        }
    }
    
    // 显示启动按钮
    func showStartButton() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        /**
            iOS 7.0 推出的，UIDynamic 物理仿真
        
            另外一个动画方法，已经过时了
            * usingSpringWithDamping: 弹力系数，0.0~1.0，数值越小，弹力越大
            * initialSpringVelocity: 初始速度，开始越快，有可能回弹
            * options: 如果不需要指定，可以设置成 nil
              JSON 序列化，位移的枚举，如果不想做任何参数，OC 可以使用 0，效率最快，swift 使用 nil
              nil(对象) & NULL(地址)
        */
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5.0, options: nil, animations: {
            self.startButton.transform = CGAffineTransformMakeScale(1.0, 1.0)
        }) { _ in }
    }
    
    @IBAction func clickStart() {
        NSNotificationCenter.defaultCenter().postNotificationName(HMSwitchRootViewControllerNotification, object: "Main")
    }
}

